package com.itheima.chat;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.ActionBarActivity;
import android.widget.TabHost;

import com.itheima.chat.fragment.ChatFragment;
import com.itheima.chat.widget.TabIndicatorView;


public class MainActivity extends ActionBarActivity implements TabHost.OnTabChangeListener
{
    private final static String TAG_CHAT     = "chat";
    private final static String TAG_CONTACT  = "contact";
    private final static String TAG_DISCOVER = "discover";
    private final static String TAG_ME       = "me";
    private FragmentTabHost mTabHost;

    private TabHost.TabSpec mChatSpec;
    private TabHost.TabSpec mContactSpec;
    private TabHost.TabSpec mDiscoverSpec;
    private TabHost.TabSpec mMeSpec;

    private TabIndicatorView mChatIndicator;
    private TabIndicatorView mContactIndicator;
    private TabIndicatorView mDiscoverIndicator;
    private TabIndicatorView mMeIndicator;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //1. tabhost的初始化
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this,
                       getSupportFragmentManager(),
                       //id指向 自定义内容部分的id
                       R.id.main_content_container);

        // 2. 新建TabSpec
        initTabSpec();

        // 3. 添加 TabSpec
        addTabSpec();

        //设置 tabhost的监听
        mTabHost.setOnTabChangedListener(this);

        //设置默认选中项
        mChatIndicator.setTabSelected(true);
    }

    private void initTabSpec()
    {
        // 2. 新建 TabSpec
        mChatSpec = mTabHost.newTabSpec(TAG_CHAT);
        // 2-1.给内容区域赋值
//        mChatSpec.setIndicator("消息");
        mChatIndicator = new TabIndicatorView(this);
        mChatIndicator.setTabIcon(R.drawable.tab_icon_chat_normal,
                                  R.drawable.tab_icon_chat_focus);
        mChatIndicator.setTabTitle("消息");
        mChatSpec.setIndicator(mChatIndicator);


        mContactSpec = mTabHost.newTabSpec(TAG_CONTACT);
        mContactIndicator = new TabIndicatorView(this);
        mContactIndicator.setTabIcon(R.drawable.tab_icon_contact_normal,
                                     R.drawable.tab_icon_contact_focus);
        mContactIndicator.setTabTitle("通讯录");
        mContactSpec.setIndicator(mContactIndicator);

        mDiscoverSpec = mTabHost.newTabSpec(TAG_DISCOVER);
        mDiscoverIndicator = new TabIndicatorView(this);
        mDiscoverIndicator.setTabIcon(R.drawable.tab_icon_discover_normal,
                                      R.drawable.tab_icon_discover_focus);
        mDiscoverIndicator.setTabTitle("发现");
        mDiscoverSpec.setIndicator(mDiscoverIndicator);

        mMeSpec = mTabHost.newTabSpec(TAG_ME);
        mMeIndicator = new TabIndicatorView(this);
        mMeIndicator.setTabIcon(R.drawable.tab_icon_me_normal,
                                R.drawable.tab_icon_me_focus);
        mMeIndicator.setTabTitle("我");
        mMeSpec.setIndicator(mMeIndicator);
    }

    private void addTabSpec()
    {
        mTabHost.addTab(mChatSpec,
                        // Class--> 对应的fragment的class类型
                        ChatFragment.class,
                        // bundle fragment新建时传入的参数
                        null);

        mTabHost.addTab(mContactSpec,
                        ChatFragment.class,
                        null);

        mTabHost.addTab(mDiscoverSpec,
                        ChatFragment.class,
                        null);

        mTabHost.addTab(mMeSpec,
                        ChatFragment.class,
                        null);
    }


    @Override
    public void onTabChanged(String tabId)
    {
        //tabId : 是新建TabSpec时的TAG

        // 让所有的不选中
        mChatIndicator.setTabSelected(false);
        mContactIndicator.setTabSelected(false);
        mDiscoverIndicator.setTabSelected(false);
        mMeIndicator.setTabSelected(false);

        if (TAG_CHAT.equals(tabId))
        {
            mChatIndicator.setTabSelected(true);
        } else if (TAG_CONTACT.equals(tabId))
        {
            mContactIndicator.setTabSelected(true);
        } else if (TAG_DISCOVER.equals(tabId))
        {
            mDiscoverIndicator.setTabSelected(true);
        } else if (TAG_ME.equals(tabId))
        {
            mMeIndicator.setTabSelected(true);
        }
    }
}
